﻿using Fishing.Abstractions;
using Fishing.Properties;
using Fishing.ViewModel;
using Hardcodet.Wpf.TaskbarNotification;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MaterialDesignThemes.Wpf;

namespace Fishing.Core
{
    public class TaskbarIconService : ITaskbarIconService
    {
        private TaskbarIcon _taskbarIcon;
        private readonly TaskbarIconViewModel _taskbarIconVm;
        private readonly ISettingsService _settingsService;
        private ContextMenu _menu;

        public TaskbarIconService(ISettingsService settingsService, TaskbarIconViewModel taskbarIconVm)
        {
            _settingsService = settingsService;
            _taskbarIconVm = taskbarIconVm;
            
            _settingsService.Settings.KeyBindingChanged += Settings_KeyBindingChanged;

            Initialize();
        }

        private void Initialize()
        {
            _taskbarIcon = new();

            _taskbarIcon.DataContext = _taskbarIconVm;

            _taskbarIcon.Icon = Images.favicon;
            _taskbarIcon.ToolTipText = Lang.Title;
            _taskbarIcon.DoubleClickCommand = _taskbarIconVm.DoubleClickCommand;
            _taskbarIcon.ContextMenu = _menu = CreateContextMenu();
        }

        public void ShowBalloonTip(string title, string message, Icon customIcon)
        {
            _taskbarIcon.ShowBalloonTip(title, message, customIcon, true);
        }

        public void SetVisibitly(Visibility visibility)
        {
            _taskbarIcon.Visibility = visibility;
        }

        private ContextMenu CreateContextMenu()
        {
            var menu = new ContextMenu();
            menu.Items.Add(new MenuItem()
            {
                Header = Lang.TextTaskbarExhaleOrHide,
                Command = _taskbarIconVm.ExhaleOrHideOrHideCommand,
                Icon = new PackIcon(){ Kind = PackIconKind.Fish, Width = 20,Height = 20 },
                InputGestureText = ConvertKeyGestureToString(_settingsService.Settings.ExhaleKeyBinding)
            });
            menu.Items.Add(new MenuItem()
            {
                Header = Lang.TextTaskbarStartUpdate,
                Command = _taskbarIconVm.StartUpdateCommand,
                Icon = new PackIcon(){ Kind = PackIconKind.Update, Width = 20,Height = 20 },
                InputGestureText = ConvertKeyGestureToString(_settingsService.Settings.QuickUpdateKeyBinding)
            });
            
            menu.Items.Add(new Separator());
            
            menu.Items.Add(new MenuItem()
            {
                Header = Lang.TextTaskbarExit,
                Command = _taskbarIconVm.ExitCommand,
                Icon = new PackIcon(){ Kind = PackIconKind.ExitToApp, Width = 20,Height = 20 },
                InputGestureText = ConvertKeyGestureToString(new KeyGesture(Key.F4, ModifierKeys.Alt))
            });

            return menu;
        }

        private static string ConvertKeyGestureToString(KeyGesture keyGesture)
        {
            var inputKeyGestureText = (int) keyGesture.Modifiers switch
            {
                1 => "Alt",
                2 => "Ctrl",
                3 => "Ctrl + Alt",
                4 => "Shift",
                5 => "Alt + Shift",
                6 => "Ctrl + Shift",
                7 => "Ctrl + Alt + Shift",
                _ => throw new NotImplementedException(),
            };

            return inputKeyGestureText + " + " + keyGesture.Key;
        }
        
        private void Settings_KeyBindingChanged(object? sender, EventArgs e)
        {
            foreach (var item in _menu.Items)
            {
                if (item is not MenuItem menuItem)
                {
                    continue;
                }
                if ((string) menuItem.Header == Lang.TextTaskbarExhaleOrHide)
                {
                    menuItem.InputGestureText = ConvertKeyGestureToString(_settingsService.Settings.ExhaleKeyBinding);
                }
                if ((string) menuItem.Header == Lang.TextTaskbarStartUpdate)
                {
                    menuItem.InputGestureText = ConvertKeyGestureToString(_settingsService.Settings.QuickUpdateKeyBinding);
                }
            }
        }
    }
}
